package com.guo.hui.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baidu.ueditor.ActionEnter;
import com.guo.hui.dao.blog.TpTimelineMapper;
import com.guo.hui.model.blog.TpTimeline;
import com.guo.hui.model.shiro.TpUser;
import com.guo.hui.service.TimeLineService;
import com.guo.hui.util.UtilJson;

@Controller
@RequestMapping("/myline")
public class MyTimeLineController extends BaseController {

	@Autowired
	private TimeLineService timelineService;
	@Autowired
	private TpTimelineMapper timelineDao;

	@RequestMapping("ueditor")
	public String ueditor(HttpServletRequest request, HttpServletResponse response) {
		try {
			response.setContentType("application/json");
			request.setCharacterEncoding("utf-8");
			response.setHeader("Content-Type", "text/html");
			String rootPath = request.getSession().getServletContext().getRealPath("/");

			String exec = new ActionEnter(request, rootPath).exec();
			// PrintWriter writer = response.getWriter();
			// writer.write(exec);
			// writer.flush();
			// writer.close();
			return exec;

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return "";
	}

	@RequestMapping("index")
	public String index() {
		return "blog/myline/index";
	}

	@ResponseBody
	@RequestMapping("linedata")
	public String linedata() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<TpTimeline> list = timelineService.queryByMap(map);
		return UtilJson.toJson(list);
	}

	@RequestMapping("lineadd")
	public String lineadd() {
		return "blog/myline/lineadd";
	}

	@ResponseBody
	@RequestMapping("/linesave")
	public String linesave(@RequestParam String title, @RequestParam String content, Integer id) {
		TpUser user = this.getUserInfo();
		timelineService.saveline(title, content, id, user);
		return "ok";
	}

	@RequestMapping("/update")
	public String update(Model mm, @RequestParam int id) {
		TpTimeline tp = timelineDao.selectById(id);
		mm.addAttribute("tp", tp);
		return "blog/myline/update";
	}

}
